<?php

class Category extends Controller {

	function Category()
	{
		parent::Controller();
		$this->load->helper('admin');
		
		construct_admin_controller($this->EzAuth_Model);
		
		
	}
	
	
	//do something before we load the page:
	//check to see if you are logged in
	
	function _remap($method) {
		checkauth($this, $method, $this->EzAuth_Model);
	}
	

	
	function index($message = '')
	{
		$data = get_page_ready('categories', 0, $this->Settings_Model, $this->Pages_Model, $this->Products_Model, $this->Categories_Model);
		
		$data['location'] = "categories";
		$data['message'] = $message;
		$this->load->view('admin/categories', $data);
	}
	
	function events($message = '')
	{
		$data = get_page_ready('categories', 0, $this->Settings_Model, $this->Pages_Model, $this->Products_Model, $this->Categories_Model);
		
		//get the end date for each event
		foreach ($data['categories'] as $category) {
			$data['enddates'][$category->catid] = $this->Categories_Model->getEndDate($category->catid);
		}
		
		$data['location'] = "events";
		$data['message'] = $message;
		$this->load->view('admin/events', $data);
	}
	
	function children() {
		$this->index();
	}
	
	function ajax_deletecategory() {
		$this->Categories_Model->delete_category($_POST['id']);
		
        }
	
	function ajax_addcategory() {
		
		//check to see if that name is taken already
		if (!$this->Categories_Model->get_catid($_POST['catname'])) {
			$cat['title'] = $_POST['catname'];
			$cat['parentid'] = $_POST['catid'];
			$cat['description'] = 'No description.';
			$newid = $this->Categories_Model->add($cat);
			
			$base_url = base_url();
			
			echo "<input type='checkbox' checked id='category[" . $newid . "]' name='category[" . $newid . "]' value='" . $newid . "' /><label for='category[" . $newid . "]'>" . $cat['title'] . "</label><br />";
		
		}

	}
	
	function delete() {
		if (isset($_POST['checkall']) && !empty($_POST['checkall'])) {
			$data = get_page_ready('areyousure',0, $this->Settings_Model, $this->Pages_Model, $this->Products_Model, $this->Categories_Model);
		
		
			//$data['deletecats'][0] = $this->Categories_Model->get_catname($this->uri->segment(4));
			$data['deletecats'] = $_POST['checkall'];
			$data['catnames'] = $this->Categories_Model->get_catname($_POST['checkall']);
			if (isset($_POST['event']))
				$data['event'] = 'TRUE';
		
			$this->load->view('admin/areyousure', $data);
		} else {
			$this->index('No categories selected!');
		}
		
	}
	
	
	function yesiamsure() {
		if (isset($_POST['categories'])) {
		foreach ($_POST['categories'] as $category) {
			$this->Categories_Model->delete_category($category);
			if (isset($_POST['event']))
			    $this->Categories_Model->delete_event($category);
		}}
		if (isset($_POST['event']))
			$this->events('Successfully deleted.');
		else
			$this->index('Successfully deleted.');
	}
	
	function add() {
		//process $_POST
		if (isset($_POST['title'])) {
			if (!isset($_POST['decription']))
				$_POST['description'] = "No description.";
			if ($_POST['title'] == "") {
				$this->index('Please fill in the title');
			} else {
				if (!$this->Categories_Model->get_catid($_POST['title'])) {
					$this->Categories_Model->add($_POST);
					$this->index('Category Added Successfully');
				} else {
					$this->index('A category by that name already exists!');
				}
			}
		} else {
			$this->index();
		}
	}
	
	function update() {
		if (isset($_POST['Cancel']) && $_POST['Cancel'] == 'Cancel') {
			$this->index();
		} else {
			//process $_POST
			if (isset($_POST['title'])) {
				if ($_POST['title'] == "" || $_POST['description'] == "") {
					$this->index('Please fill in the title and description');
				} else {
					$this->Categories_Model->update($_POST);
					$this->index('Category Updated Successfully');
				}
			} else {
				$this->index();
			}
		}
	}
	
	function eventadd() {
		/*
		 $_POST["title"]
		 $_POST["description"]
		 $_POST["startdate"]
		 $_POST["enddate"]
		 $_POST["eventtype"]=> "percentage" or "dollar"
		 $_POST["percentagediscount"]
		 $_POST["dollardiscount"]
		*/
		
		//process $_POST
		if (isset($_POST['title'])) {
			if (!isset($_POST['title']) || !isset($_POST['description']) || !isset($_POST['eventtype']) || !isset($_POST['startdate']) || !isset($_POST['enddate'])) {
				$this->events('Please fill out the entire form to create an event');
			} else {
				if ($_POST['eventtype'] == "percentage" && $_POST['percentagediscount'] == "") {
					$this->events('Please specify the percentage discount');
				} else {
					if ($_POST['eventtype'] == "dollar" && $_POST['dollardiscount'] == '') {
						$this->events('Please specify the dollar amount discount');
					} else {
					
						if (!$this->Categories_Model->get_catid($_POST['title'])) {
							
							//create the category
							$_POST['parentid'] = 99;
							
							//add the event
							
							$this->Categories_Model->eventadd($_POST);
							
							$this->events('Event Added Successfully');
						} else {
							$this->Events('An event by that name already exists!');
						}
					}
				}
			}
		} else {
			$this->events();
		}
		
		
	}
	
	function eventupdate() {
		if (isset($_POST['Cancel']) && $_POST['Cancel'] == 'Cancel') {
			$this->index();
		} else {
			//process $_POST
			if (isset($_POST['title'])) {
				if ($_POST['title'] == "" || $_POST['description'] == "" || $_POST['eventtype'] == "" || $_POST['startdate'] == "" || $_POST['enddate'] == "") {
					$this->events('Please fill out the entire form to create an event');
				} else {
					if ($_POST['eventtype'] == "percentage" && $_POST['percentagediscount'] == "") {
						$this->events('Please specify the percentage discount');
					} else {
						if ($_POST['eventtype'] == "dollar" && $_POST['dollardiscount'] == '') {
							$this->events('Please specify the dollar amount discount');
						} else {
							$this->Categories_Model->eventupdate($_POST);
							$this->events('Event Updated Successfully');
			
						}
					}
				}
			} else {
				$this->events();
			}
		}
	}
	
	function eventedit() {
		$data = get_page_ready('categories', 0, $this->Settings_Model, $this->Pages_Model, $this->Products_Model, $this->Categories_Model);
		
		$data['location'] = "events";
		$data['edit'] = $this->Categories_Model->getevent($this->uri->segment(4));
		
		$cat = $this->Categories_Model->get_category_title_and_description($this->uri->segment(4));
		
		$data['edit']->title = $cat->title;
		$data['edit']->description = $cat->description;
		
		//get the end date for each event
		foreach ($data['categories'] as $category) {
			$data['enddates'][$category->catid] = $this->Categories_Model->getEndDate($category->catid);
		}
		
		$this->load->view('admin/events', $data);
	}
	
	function edit() {
		//we are editing $this->uri->segment(4);
		
		//we want to populate the Add Category form with this category's stuff,
		//including a hidden field with the category's id
		//and change the word 'Add' to 'Edit'
		//and the submit button to say 'Save Changes'
		
		$data = get_page_ready('categories', 0, $this->Settings_Model, $this->Pages_Model, $this->Products_Model, $this->Categories_Model);
		
		$data['location'] = "categories";
		$data['edit'] = '';
		$data['edit']->id = $this->uri->segment(4);
		
		foreach ($data['categories'] as $category) {
			if ($category->catid == $this->uri->segment(4)) {
				$data['edit']->title = $category->catname;
				$data['edit']->description = $category->description;
				$data['edit']->parentid = $category->parentid;
				break;
			}
		}
		
		if (isset($data['edit']->title)) {
			$this->load->view('admin/categories', $data);
		} else {
			$this->index('No such category to edit!');
		}
	}
	

}
?>